package com.yilin.medical.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;

public class PushUtil {
	// 获取ApiKey
    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                apiKey = metaData.getString(metaKey);
            }
        } catch (NameNotFoundException e) {

        }
        return apiKey;
    }
	  public static String logStringCache = "";
	 // 用share preference来实现是否绑定的开关。在ionBind且成功时设置true，unBind且成功时设置false
    public static boolean hasBind(Context context) {
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(context);
        String flag = sp.getString("bind_flag", "");
        if ("ok".equalsIgnoreCase(flag)) {
            return true;
        }
        return false;
    }

    public static void setBind(Context context, boolean flag) {
        String flagStr = "not";
        if (flag) {
            flagStr = "ok";
        }
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(context);
        Editor editor = sp.edit();
        editor.putString("bind_flag", flagStr);
        editor.commit();
    }
    
    public static void saveUserIdChannelId(Context context,String pushUserId,String pushChannelId){
    	
    	SharedPreferences settings =context.getSharedPreferences("jx_zhagen", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("pushUserId", pushUserId);
		editor.putString("pushChannelId", pushChannelId);
		editor.commit();
    }
    
    /**
     * 获取pushUserId
     * @param context
     * @return
     */
    public static String getPushUserId(Context context){
    	SharedPreferences settings =context.getSharedPreferences("jx_zhagen", Context.MODE_PRIVATE);
    	return settings.getString("pushUserId", "0");
    }
    
    /**
     * 获取pushChannelId
     * @param context
     * @return
     */
    public static String  getPushChannelId(Context context){
    	SharedPreferences settings =context.getSharedPreferences("jx_zhagen", Context.MODE_PRIVATE);
    	return settings.getString("pushChannelId", "0");
    }
}
